﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace KBMVC.Core
{
    public class Article : IDataErrorInfo, Interfaces.IRetainable
    {
        public string Title { get; set; }
        public string Slug { get; set; }
        public DateTime CreationDate { get; set; }
        public string Author { get; set; }
        public string Content { get; set; }
        public string Tags { get; set; }

        #region Constructors

        public Article()
        {

        }

        public Article( string ArticleSlug )
        {
            KBMVC.Core.Article tempArticle = Data.Article.Load(ArticleSlug);
            if (tempArticle != null)
            {
                this.Title = tempArticle.Title;
                this.Slug = tempArticle.Slug;
                this.Author = tempArticle.Author;
                this.Tags = tempArticle.Tags;
                this.CreationDate = tempArticle.CreationDate;
                this.Content = tempArticle.Content;
            }
        }

        #endregion

        #region URL Slug Generator

        //http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html
        //turns any string into a URL Slug string
        private static string GenerateSlug(string phrase)
        {
            string str = RemoveAccent(phrase).ToLower();

            str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars           
            str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space   
            str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it   
            str = Regex.Replace(str, @"\s", "-"); // hyphens   

            return str;
        }

        //http://predicatet.blogspot.com/2009/04/improved-c-slug-generator-or-how-to.html
        //helper method to GenerateSlug
        private static string RemoveAccent(string txt)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
            return System.Text.Encoding.ASCII.GetString(bytes);
        }

        #endregion

        #region IDataErrorInfo Implementation

        public string Error { get { return null; } }

        public string this[string propName]
        {
            get
            {
                if ((propName == "Title") && string.IsNullOrEmpty(Title))
                    return "Please enter a title.";
                return null;
            }
        }

        #endregion

        #region Implement IRetainable Interface Methods

        public bool Save()
        {
            //replace file location below to match your specific install
            return Data.Article.Save(this);
        }

        public bool Delete()
        {
            //replace file location below to match your specific install
            return Data.Article.Delete(this);
        }

        #endregion
    }
}
